//============================================================================
// Name        : Stringline.c
// Author      : Michael Gower, Sameer Jamal, Kevin Ross
// Version     :
// Copyright   : GPL
// Description : Program to interpret Stringline language
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include "hw2.tab.h"
#include "semantics/LookupTable.h"

extern FILE* yyin;
extern FILE* yyout;
extern int hw2_parseError;

extern pLookupTable table;


int main(int argc, char** argv) 
{
	if (argc == 2)
	{
		FILE* pFile = fopen(argv[1], "r");
		if (pFile)
		{
			yyin = pFile;
			yyrestart(yyin);
			yyout = stdout;
			printf("Parsing file: %s\n", argv[1]);
		}
		else
		{
			printf("Unable to open file: %s\n", argv[1]);
			return 1; // fail
		}
	}

	// semanticsInit creates table, used in yyparse() and below
	if (!semanticsInit()) return 1; // fail
	yyparse();
	if (hw2_parseError)
	{
		printf("Syntax Error in Parse.\n");
		return 1; // fail
	}
	printf("Parse successful!\n");

	pAnonymousStatement sl_main = table->getMain(table);
	if (sl_main == NULL) return 0; // empty file, success
	sl_main->eval(sl_main);

	return 0; // no parse error, success
}
